package edebe.flyinginstrument.render;

import edebe.flyinginstrument.entity.EntityFlyingInstrument;
import edebe.flyinginstrument.loader.ItemLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

@SideOnly(Side.CLIENT)
public class RenderFlyingInstrument extends Render<EntityFlyingInstrument> {
    public RenderFlyingInstrument(RenderManager renderManager) {
        super(renderManager);
    }

    @Override
    public void doRender(@Nonnull EntityFlyingInstrument entity, double x, double y, double z, float entityYaw, float partialTicks) {
        World world = entity.world;
        if (world == null) return;
        GlStateManager.pushMatrix();
        RenderItem itemRender = Minecraft.getMinecraft().getRenderItem();
        ItemStack itemStack = entity.getItemStack();
        this.bindEntityTexture(entity);

        Item item = itemStack.getItem();
        if (item == ItemLoader.GOURD) {
            GlStateManager.scale(2.0f, 2.0f, 2.0f);
            GlStateManager.translate(x, y + 0.55, z);
            GlStateManager.rotate(180, 1, 1, 0);
            GlStateManager.rotate(90, 1, 0, 0);
            GlStateManager.rotate(-entityYaw + 180, 1, 0, 0);
        } else if (item == ItemLoader.LEAF) {
            GlStateManager.scale(2.0f, 2.0f, 2.0f);
            GlStateManager.translate(x, y + 0.5, z);
            GlStateManager.rotate(-entityYaw + 180, 0, 1, 0);
        } else if (item == ItemLoader.BOAT) {
            GlStateManager.scale(2.0f, 2.0f, 2.0f);
            GlStateManager.translate(x, y + 0.58, z);
            GlStateManager.rotate(-entityYaw + 180, 0, 1, 0);
        } else if (item == ItemLoader.MOUNTAIN_AND_RIVER_DIAGRAM) {
            GlStateManager.scale(2.0f, 2.0f, 2.0f);
            GlStateManager.translate(x, y + 0.535, z);
            GlStateManager.rotate(-entityYaw + 180, 0, 1, 0);
        } else {
            GlStateManager.translate(x, y + 1.2, z);
            GlStateManager.rotate(-45, 0, 0, 1);
            GlStateManager.rotate(90, 1, 1, 0);
            GlStateManager.rotate(entityYaw + 90, 0, 0, 1);
        }

        itemRender.renderItem(itemStack, itemRender.getItemModelWithOverrides(itemStack, null, null));
        //itemRender.renderItem(itemStack, entity, ItemCameraTransforms.TransformType.HEAD, false);
        GlStateManager.popMatrix();
        super.doRender(entity, x, y, z, entityYaw, partialTicks);
    }

    @Nullable
    @Override
    protected ResourceLocation getEntityTexture(@Nullable EntityFlyingInstrument entity) {
        return TextureMap.LOCATION_BLOCKS_TEXTURE;
    }
}